//
// Created by hanweiguang on 2019-02-16.
// Copyright (c) 2019 hanweiguang. All rights reserved.
//

#import "AdCell.h"
#import "AdModel.h"

#define kHeightOfStatusBar 20
#define kHeightOfAssemble 44
#define kHeightOfNavigationAndStatusBar 64
#define kBoundsOfScreen [[UIScreen mainScreen] bounds]
#define kWidthOfScreen [[UIScreen mainScreen] bounds].size.width
#define kHeightOfScreen [[UIScreen mainScreen] bounds].size.height

@implementation AdCell

// 重写父类的方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 初始化视图
        // 1. 滚动视图
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidthOfScreen, 160)];
        [self.contentView addSubview:_scrollView];
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;

        // 2. 背景视图
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 130, kWidthOfScreen, 30)];
        _bgView.backgroundColor = [UIColor grayColor];
        // 设置透明度
        _bgView.alpha = 0.5;
        [self.contentView addSubview:_bgView];

        // 3. 标题文字
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 130, 120, 30)];
        _titleLabel.textColor = [UIColor whiteColor];
        [self.contentView addSubview:_titleLabel];

        // 4. pageCtrl
        _pageCtrl = [[UIPageControl alloc] initWithFrame:CGRectMake(160, 130, kWidthOfScreen - 180, 30)];
        [self.contentView addSubview:_pageCtrl];
    }
    return self;
}

// 重新实现setter方法
- (void)setAdArray:(NSArray *)adArray {
    _adArray = adArray;

    // 添加额外的功能
    // 1.滚动视图: 显示4张图片
    for (int i = 0; i < adArray.count; i++) {
        // 创建图片视图对象
        AdModel *model = adArray[i];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * kWidthOfScreen, 0, kWidthOfScreen, CGRectGetHeight(self.scrollView.frame))];
        imageView.image = [UIImage imageNamed:model.imageName];
        //UIViewContentModeScaleToFill：图片被拉伸（比例可能不变）以充满整个imageView
        //UIViewContentModeScaleAspectFit：图片被拉伸，比例不变，图片的长边刚好与imageview一样
        //UIViewContentModeScaleAspectFill:图片被拉伸，比例不变，图片的短边与imageview一致，图片有可能超出imageview
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [self.scrollView addSubview:imageView];
    }
    self.scrollView.contentSize = CGSizeMake(kWidthOfScreen * adArray.count, 160);

    // 2. 背景视图无需改动

    // 3. 标题文字
    if (adArray.count > 0) {
        AdModel *model = adArray[0];
        // 显示标题
        self.textLabel.text = model.title;
    }

    // 4. pageCtrl
    _pageCtrl.numberOfPages = adArray.count;
    _pageCtrl.currentPage = 0;
}

#pragma mark - 代理实现

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 计算当前滑动到第几页
    int page = scrollView.contentOffset.x / CGRectGetWidth(scrollView.bounds);
    self.detailTextLabel.text = [self.adArray[page] title];
    self.pageCtrl.currentPage = page;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
